const uuid = require('uuid')

module.exports = async (ctx, next) => {
  // 生成请求ID
  ctx.state.requestId = `req_${uuid.v4().split('-')[0]}`

  // 记录请求开始时间
  ctx.state.startTime = Date.now()

  await next()

  // 记录响应时间
  if (ctx.body) {
    ctx.body.latency = `${Date.now() - ctx.state.startTime}ms`
  }
}
